home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / rsynth / src / darray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  1.3 KB  |  82 lines

  1. #include <stddef.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include "proto.h"
  5. #include "darray.h"
  6.  
  7. void
  8. darray_free(a)
  9. darray_t *a;
  10. {
  11.  if (a->data)
  12.   {
  13.    free(a->data);
  14.    a->data = NULL;
  15.   }
  16.  a->items = a->alloc = 0;
  17. }
  18.  
  19. void *
  20. Darray_find(a, n)
  21. darray_t *a;
  22. unsigned n;
  23. {
  24.  if (n >= a->alloc || n >= a->items)
  25.   {
  26.    unsigned osize = a->items * a->esize;
  27.    unsigned nsize;
  28.    if (!a->esize)
  29.     abort();
  30.    if (n >= a->alloc)
  31.     {
  32.      unsigned add = (a->get) ? a->get : 1;
  33.      char *ndata = (char *) malloc(nsize = (n + add) * a->esize);
  34.      if (ndata)
  35.       {
  36.        if (osize)
  37.         memcpy(ndata, a->data, osize);
  38.        if (a->data)
  39.         free(a->data);
  40.        a->data = ndata;
  41.        a->alloc = n + add;
  42.       }
  43.      else
  44.       return NULL;
  45.     }
  46.    else
  47.     nsize = (n + 1) * a->esize;
  48.    if (n >= a->items)
  49.     {
  50.      memset(a->data + osize, 0, nsize - osize);
  51.      a->items = n + 1;
  52.     }
  53.   }
  54.  return (void *) (a->data + n * a->esize);
  55. }
  56.  
  57. int
  58. darray_delete(a, n)
  59. darray_t *a;
  60. unsigned n;
  61. {
  62.  char *p = (char *) darray_find(a, n);
  63.  if (p)
  64.   {
  65.    if (a->items)
  66.     {
  67.      a->items--;
  68.      while (n++ < a->items)
  69.       {
  70.        memcpy(p, p + a->esize, a->esize);
  71.        p += a->esize;
  72.       }
  73.      memset(p, 0, a->esize);
  74.      return 1;
  75.     }
  76.    else
  77.     abort();
  78.   }
  79.  else
  80.   return 0;
  81. }
  82.